<?php
/**
 * Created by PhpStorm.
 * User: 小蛮哼哼哼
 * Email: 243194993@qq.com
 * Date: 2021/12/28
 * Time: 20:58
 * motto: 现在的努力是为了小时候吹过的牛逼！
 */

declare(strict_types=1);

namespace app\web\controller;

use app\common\business\Wechat;
use app\common\business\Yu;
use app\common\lib\Time;
use think\Exception;
use think\facade\View;

class Home extends WechatBase
{

    public function index()
    {
//        halt(config('key.session_key'));
        try {
            $wechat = (new Wechat())->getOpenidById($this->openid);
        } catch (\Exception $e) {
            throw new Exception("查询失败");
        }
        if (!$wechat) {
            return View::fetch('home/error');
        }
        if ($wechat['is_staff'] != 1) {
            return View::fetch('home/error',[
                "id"=>$wechat['id']
            ]);
        }
        $config = $this->wechat_app->jssdk->buildConfig(['updateAppMessageShareData', 'updateTimelineShareData'], false);
//        halt($config);
        View::assign('config', $config);
        //根据openid查询是否为员工
        View::assign('openid', $this->openid);
        //查询数据
        $dateTime = $this->request->param('dateTime','','trim');
        if(empty($dateTime)){
            $where[] = [
                ['create_time','between',Time::month()]
            ];
            View::assign('dateTime',date('Y-m'));
        }else{
            $start= strtotime(date('Y-m-1 00:00:00',strtotime($dateTime)));
            $end = strtotime(date('Y-m-'.date('t',strtotime($dateTime))." 23:59:59",strtotime($dateTime)));

            $where[] = [
                ['create_time','between',[$start,$end]]
            ];
            View::assign('dateTime',$dateTime);
        }
        $lists = (new Yu())->getHomeByIndexPage($this->openid,$where);
        $count = (new Yu())->getHomeByCount($this->openid,$where);
        return View::fetch('', [
            'lists' => $lists->toArray(),
            "page" => $lists->render(),
            "count" => $count
        ]);
    }
}